<?php

namespace App\Models;

use App\Models\Traits\HasTreeStructure;
use App\Models\Traits\HasStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Department extends Model
{
    use HasFactory, SoftDeletes, HasTreeStructure, HasStatus;

    protected $fillable = [
        'parent_id',
        'name',
        'code',
        'leader',
        'phone',
        'email',
        'status',
        'sort',
        'remark',
    ];

    protected $casts = [
        'parent_id' => 'integer',
        'status' => 'integer',
        'sort' => 'integer',
    ];

    public function parent()
    {
        return $this->belongsTo(Department::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Department::class, 'parent_id')->orderBy('sort');
    }

    public function users()
    {
        return $this->hasMany(User::class, 'dept_id');
    }
}

